package com.arcsoft.commonbase.widgets

import android.app.AlertDialog
import android.content.Context
import android.content.DialogInterface
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.ProgressBar
import android.widget.TextView
import com.arcsoft.commonbase.R
import com.arcsoft.commonbase.ext.visibleOrGone
import com.arcsoft.commonbase.ext.visibleOrInvisible

class ProgressDialogVer(context:Context): AlertDialog(context, R.style.ProgressStyle) {

    private var mProgress: ProgressBar? = null
    private var mIndeterminate = false
    private var mMessageView: TextView? = null

    private var mProgressDrawable: Drawable? = null
    private var mIndeterminateDrawable: Drawable? = null
    private var mMessage: CharSequence? = null

    companion object {
        fun show(
            context: Context,
            title: CharSequence,
            message: CharSequence,
            indeterminate: Boolean,
            cancelable: Boolean
        ): ProgressDialogVer {
            return show(context, title, message, indeterminate, cancelable, null)
        }

        fun show(
            context: Context,
            title: CharSequence,
            message: CharSequence,
            indeterminate: Boolean,
            cancelable: Boolean,
            cancelListener: DialogInterface.OnCancelListener?
        ): ProgressDialogVer {
            return ProgressDialogVer(context).apply {
                setTitle(title)
                setMessage(message);
                setIndeterminate(indeterminate);
                setCancelable(cancelable);
                setOnCancelListener(cancelListener);
                show()
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        val view = LayoutInflater.from(context).inflate(R.layout.progress_dialog, null);
        mProgress = view.findViewById(R.id.progress)
        mMessageView = view.findViewById(R.id.message)
        setView(view)
        mProgressDrawable?.let {
            setProgressDrawable(it)
        }
        mIndeterminateDrawable?.let {
            setIndeterminateDrawable(it)
        }
        mMessage?.let {
            setMessage(it)
        }

        setIndeterminate(mIndeterminate)

        super.onCreate(savedInstanceState)
    }

    fun setProgressDrawable(d: Drawable) {
        mProgress?.let {
            it.progressDrawable = d
            return
        }
        mProgressDrawable = d
    }

    fun setIndeterminateDrawable(drawable: Drawable) {
        mProgress?.let {
            it.indeterminateDrawable = drawable
            return
        }
        mIndeterminateDrawable = drawable
    }


    fun setIndeterminate(indeterminate:Boolean) {
        mProgress?.let {
            it.isIndeterminate = indeterminate
            return
        }
        mIndeterminate = indeterminate
    }

    override fun setMessage(message: CharSequence?) {
        mMessageView?.let {
            it.text = message
            it.visibleOrInvisible(!message.isNullOrEmpty())
            return
        }
        mMessage = message
    }
}